package com.km.team.phsystem.msg.controller;

import com.km.team.phsystem.beans.vo.ResponseData;
import com.km.team.phsystem.common.enums.ReservationTypeEnum;
import com.km.team.phsystem.common.enums.ResultEnum;
import com.km.team.phsystem.msg.IdentifyResponse;
import com.km.team.phsystem.msg.service.SmsServiceImpl;
import com.km.team.phsystem.web.controller.base.BaseController;
import com.km.team.phsystem.web.utils.ResponseDataUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;


/**
 * 提供短信有关的接口或服务
 */
@RestController
@RequestMapping("/sms")
public class SmsController extends BaseController<SmsController> {


    @PostMapping("/send_notice")
    @ApiOperation("发送短信")
    public ResponseData sendSms(@RequestParam("phoneNo") String phoneNo, @RequestParam("dateTime") String dateTime, @RequestParam("number") String number, @RequestParam("businessType") String businessType) throws Exception{
        String result = smsService.sendNoticeSms(phoneNo, dateTime, number, businessType,ReservationTypeEnum.RESERVATION_TYPE_FIXED_APPOINTMENT);

        if(result.equals("success")){
            return ResponseDataUtil.buildSuccess();
        }else{
            return ResponseDataUtil.buildError(result);
        }

    }


    /**
     * 随机生成验证码并插表
     * @param phoneNo 电话号码
     * @return 发送结果
     * @throws Exception 异常，会被advice捕捉
     */
    @PostMapping("/send_identify")
    @ApiOperation("发送验证码")
    public ResponseData sendIdentify(@RequestParam("phoneNo") String phoneNo) throws Exception{
        IdentifyResponse response = smsService.sendIdentifySms(phoneNo);
        if(!(response.getCode() == null)){
            return ResponseDataUtil.buildSuccess(response.getCode());
        }
        return ResponseDataUtil.buildError(ResultEnum.ERROR.getCode(), response.getMessage());
    }


    /**
     * 验证用户填写的验证码是否有效
     * @param phoneNo 电话号码
     * @param code 验证码
     * @return 是否有效
     * @throws Exception 异常，会被advice捕捉
     */
    @GetMapping("/verify_code")
    @ApiOperation("验证码有效性查证")
    public ResponseData verifyCode(@RequestParam("phoneNo") String phoneNo, @RequestParam("code") String code) throws Exception{
        if(smsService.verifyIdentifyCode(phoneNo, code).equalsIgnoreCase("success")){
            smsService.disableIdentifyStatus(phoneNo, code);
            return ResponseDataUtil.buildSuccess("valid");
        }
        return ResponseDataUtil.buildError("invalid");
    }

}
